package com.gzc.just.play.last.war.allinone.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * All-in-One 服务器初始化器
 * 确保所有必需的服务器 Bean 被正确加载
 */
@Component
public class AllInOneServerInitializer implements ApplicationListener<ApplicationContextInitializedEvent> {
    private static final Logger logger = LoggerFactory.getLogger(AllInOneServerInitializer.class);
    
    @Override
    public void onApplicationEvent(ApplicationContextInitializedEvent event) {
        logger.info("Initializing All-in-One servers...");
        
        try {
            // 尝试获取 SceneNettyServer Bean 来确保它被初始化
            Object sceneServer = event.getApplicationContext().getBean("sceneNettyServer");
            logger.info("✓ SceneNettyServer bean successfully initialized");
        } catch (Exception e) {
            logger.warn("SceneNettyServer bean not found or initialization failed: {}", e.getMessage());
        }
        
        try {
            // 尝试获取 BalanceNettyServer Bean
            Object balanceServer = event.getApplicationContext().getBean("balance-balanceNettyServer");
            logger.info("✓ BalanceNettyServer bean successfully initialized");
        } catch (Exception e) {
            logger.warn("BalanceNettyServer bean not found or initialization failed: {}", e.getMessage());
        }
        
        try {
            // 检查机器人客户端是否启用并初始化
            boolean robotEnabled = Boolean.parseBoolean(
                System.getProperty("robot.client.enabled", "false"));
            
            if (robotEnabled) {
                Object robotManager = event.getApplicationContext().getBean("robotManager");
                if (robotManager != null) {
                    logger.info("✓ RobotManager bean successfully initialized");
                }
            } else {
                logger.info("Robot client is disabled");
            }
        } catch (Exception e) {
            logger.info("Robot client not configured or initialization skipped");
        }
    }
}
